from selenium.webdriver.support.wait import WebDriverWait
from appium.webdriver.common.appiumby import By

from appPage.enter_homepage import EnterHomePage
from appPage.login import Login
from config import PHONE, PWD, SEARCH_TEXT
from utils.driver_utils import DriverUtils


class Search:
    def search(self, driver, search_text):
        WebDriverWait(driver, 10).until(lambda x: x.find_element(By.ID, "com.tpshop.malls:id/home_img")).click()
        WebDriverWait(driver, 10).until(lambda x: x.find_element(By.ID, "com.tpshop.malls:id/default_search_et")).click()
        WebDriverWait(driver, 10).until(lambda x: x.find_element(By.ID, "com.tpshop.malls:id/search_et")).send_keys(search_text)
        WebDriverWait(driver, 10).until(lambda x: x.find_element(By.ID, "com.tpshop.malls:id/search_btn")).click()
        WebDriverWait(driver, 10).until(lambda x: x.find_element(By.ID, "com.tpshop.malls:id/product_pic_img")).click()


if __name__ == '__main__':
    # 驱动
    driver_utils = DriverUtils()
    driver = driver_utils.get_driver()
    # 进入首页
    enter = EnterHomePage()
    # 登录
    login = Login()
    # 搜索
    search = Search()
    enter.enter_homepage(driver)
    login.login(driver, PHONE, PWD)
    search.search(driver, SEARCH_TEXT)
    driver_utils.quit_driver()

